from tortoise import fields
from tortoise.models import Model
from core.security import hash_password

class Admin(Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=32, unique=True, index=True)
    password_hash = fields.CharField(max_length=128)
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "admin"

    @classmethod
    async def create_admin(cls, username: str, raw_password: str):
        """工厂方法：创建管理员，自动哈希密码"""
        return await cls.create(username=username, password_hash=hash_password(raw_password))

    def verify_password(self, raw: str) -> bool:
        from core.security import verify_password
        return verify_password(raw, self.password_hash)